<?php
declare (strict_types = 1);

namespace app\middleware;
use app\mine\jwtService;


class CheckToken
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $token =   $request->header('Authorization');
        if (!$token) {
            return json(['error_code' => 401, 'msg' => 'token不能为空', 'data' => ''], 401);
        }
        $jwtService = new jwtService();
        $uid = $jwtService->verifyToken($token, 'your_secret_key');
        if ($uid === null) {
            return json(['error_code' => 999, 'msg' => 'token验证失败', 'data' => ''], 401);
        }
        $request->uid = $uid;
        return $next($request);
    }
}
